import MapInfo from "../map/MapInfo"


export default class MapUtil {
    public static pixelToGrid(pos: cc.Vec2): cc.Vec2 {
        const x = Math.floor(pos.x / MapInfo.gridWidth)
        const y = Math.floor(pos.y / MapInfo.gridHeight)

        return cc.v2(x, y)
    }

    public static gridToPos(x: number, y: number): cc.Vec2 {
        return cc.v2(x * MapInfo.gridWidth + MapInfo.gridWidth / 2, y * MapInfo.gridHeight + MapInfo.gridHeight / 2)
    }


    public static pixelToPos(pos: cc.Vec2): cc.Vec2 {
        const grid = this.pixelToGrid(pos)

        return this.gridToPos(grid.x, grid.y)
    }

    public static pixelToGridText(pos: cc.Vec2): string {
        const grid = this.pixelToGrid(pos)

        return this.xyToGridText(grid.x, grid.y)
    }

    public static xyToGridText(x: number, y: number): string {
        return `${x}_ ${y}`
    }
}
